<?php

namespace App\Models;

use App\Exceptions\CustomException;
use App\Http\Resources\ChooseStoresCollection;
use App\Http\Resources\StoreCollection;
use App\ModelFilters\StoreFilter;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use App\Http\Resources\Store as StoreResource;

class Store extends Base
{
    const STATUS_UNCHECK       = 0;
    const STATUS_CHECK_FAIL    = 1;
    const STATUS_CHECK_SUCCESS = 2;
    const STATUS_MAP = [
        0 => '待审核',
        1 => '审核拒绝',
        2 => '审核通过',
    ];
    protected function add($inputs)
    {
        $inputs['aid'] = auth()->id();
        $this->fill($inputs);
        $this->save();
    }

    protected function index($inputs)
    {
        $inputs['aids'] = array_merge(AdminUser::getSubUserIds(auth()->id()), [auth()->id()]);
        return new StoreCollection(
            $this->filter($inputs, StoreFilter::class)
                ->orderByDesc('created_at')->paginate());
    }

    protected function indexByAuth()
    {
        $aid = [
            auth()->user()->pid,
            auth()->user()->id
        ];
        $store = Store::with(['admin:id,name'])->whereIn('aid', $aid)
            ->where('status', Store::STATUS_CHECK_SUCCESS)
            ->select(['id', 'aid', 'logo', 'name', 'legal_person'])->get();
        $data = (new ChooseStoresCollection($store))->response()->getData(true);
        return $data;
    }

    protected function checkStoreIndex($inputs)
    {
        $inputs['status'] = self::STATUS_UNCHECK;
        return new StoreCollection(
            $this->filter($inputs, StoreFilter::class)
                ->orderByDesc('created_at')->paginate());
    }

    protected function checkStore($inputs)
    {
        return $this->where('id', $inputs['id'])->update($inputs);
    }

    protected function edit($inputs)
    {
        $inputs['status'] = self::STATUS_UNCHECK;
        return $this->where('id', $inputs['id'])->update($inputs);
    }

    protected function setStorePass($inputs)
    {
        return $this->where('id', $inputs['id'])->update(['store_pass' => bcrypt($inputs['store_pass'])]);
    }

    protected function login($inputs)
    {
        $store = $this->find($inputs['id']);
        if (! $store->store_pass) {
            throw new CustomException(1003, '此商店尚未设置密码,请联系管理员设置');
        }
        if (! Hash::check($inputs['store_pass'], $store->store_pass)) {
            throw new CustomException(1004, '密码错误');
        }
        $token = md5($inputs['id'] . $inputs['store_pass'] . time() . rand(1000, 9999));
        Cache::put($token, $store->toJson(), 60*60*3);
        return successReturn('登录成功', compact('token'));
    }

    protected function info($inputs)
    {
        return successReturn('获取成功', (new StoreResource($this->find($inputs['id'])))->response()->getData(true));
    }

    public function admin()
    {
        return $this->belongsTo(AdminUser::class, 'aid', 'id');
    }
}
